
function xx = Bcr4bp_Iner2RotMtx2D(tt , XX , pc , coord , aux)
%
% BCR4BP坐标转换，Inertial -> Rot，2D，矩阵计算
%
% 输入：
% tt：       时间 , N x 1
% xx：      旋转系状态 , N x 6
% pc_ini：惯性系中心 , 1（P1 centered）/ 2（P2 centered） , 1 x 1
% aux：    仿真参数 , 1 x 1, 结构体
% coord：坐标系, 'EMrot' / 'SB1rot'
%
% 参考：
% 2021_ZimovanSpreen, page 37-40
%
% 时间：2024年06月03日
% 作者：张晨
% 单位：CSU
% 邮箱：chenzhang@csu.ac.cn
%%%%%%%%%%%%%%%%%%%%%%%

mu = aux.EMRot.mu;
mus = aux.EMRot.mus;
as = aux.EMRot.as;
wm = aux.SB1Rot.wm;

if size(XX , 2) ~= 4
    disp(' ========== wrong! xx row num! ========== ')
    xx = [];
    return;
end

if size(tt , 2) ~= 1
    disp(' ========== wrong! tt row num! ========== ')
    xx = [];
    return;
end

sin_tt = sin(tt);
cos_tt = cos(tt);

X = XX(: , 1);
Y = XX(: , 2);
VX = XX(: , 3);
VY = XX(: , 4);

if strcmp(coord , 'emrot')

    if strcmp(pc , 'p1')
        % P1 centered

        xx = [X .* cos_tt - mu + Y .* sin_tt, ...
            Y .* cos_tt - X .* sin_tt, ...
            VX .* cos_tt + Y .* cos_tt + VY .* sin_tt - X .* sin_tt, ...
            VY .* cos_tt - X .* cos_tt - VX .* sin_tt - Y .* sin_tt];

    elseif strcmp(pc , 'p2')
        % P2 centered

        xx = [X .* cos_tt - mu + Y .* sin_tt + 1, ...
            Y .* cos_tt - X .* sin_tt, ...
            VX .* cos_tt + Y .* cos_tt + VY .* sin_tt - X .* sin_tt, ...
            VY .* cos_tt - X .* cos_tt - VX .* sin_tt - Y .* sin_tt];

    else

        fprintf('wrong!')
        xx = [];
        return;

    end

elseif strcmp(coord , 'sb1rot')

    if strcmp(pc , 'p1')
        % P1 centered

        xx = [X*cos_tt + Y*sin_tt + mus/(mus + 1) - (mu*cos(tt*wm))/as, ...
            Y*cos_tt - X*sin_tt - (mu*sin(tt*wm))/as, ...
            VX*cos_tt + Y*cos_tt + VY*sin_tt - X*sin_tt + (mu*wm*sin(tt*wm))/as, ...
            VY*cos_tt - X*cos_tt - VX*sin_tt - Y*sin_tt - (mu*wm*cos(tt*wm))/as];

    elseif strcmp(pc , 'p2')
        % P2 centered

        xx = [X*cos_tt + Y*sin_tt + mus/(mus + 1) - (cos(tt*wm)*(mu - 1))/as, ...
            Y*cos_tt - X*sin_tt - (sin(tt*wm)*(mu - 1))/as, ...
            VX*cos_tt + Y*cos_tt + VY*sin_tt - X*sin_tt + (wm*sin(tt*wm)*(mu - 1))/as, ...
            VY*cos_tt - X*cos_tt - VX*sin_tt - Y*sin_tt - (wm*cos(tt*wm)*(mu - 1))/as];

    else

        fprintf('wrong!')
        xx = [];
        return;

    end

end

end
